/**
 *
 */
package com.yidu.menuProject.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.menuProject.action.vo.FoodVo;
import com.yidu.menuProject.common.ResultMap;
import com.yidu.menuProject.commont.Tools;
import com.yidu.menuProject.model.EhyManagerModel;
import com.yidu.menuProject.model.FoodModel;
import com.yidu.menuProject.service.FoodService;
/**
 * @ author pengyong
 * @date 2018年7月13日 下午4:00:30
 * @ version 1.0
 */
@Controller
@RequestMapping("/food")
public class Foodaction {
	@Resource
	private FoodService foodservice;
	
	
	
	/*@RequestMapping("/selectfoodAll")
	public String selectAlls(String manUser,String manPwd,HttpServletRequest request){
		MealModel mm = foodservice.selectfoodAlls(manUser, manPwd);
		FoodVo food = new FoodVo();
		int count = foodservice.selectcount(food);
		if(mm!=null){
			HttpSession session = request.getSession();
			session.setAttribute("mm", mm);
			return "redirect:/pages/orderbackground/food2.html";
		}else{
			request.setAttribute("message", "用户名或密码错误");
			return "login";
		}
	}*/
	
	
	
	@RequestMapping("/selectfoodAlls")
	@ResponseBody
	public ResultMap<List<FoodModel>> backContent(FoodVo vo,String foodName,@RequestParam("limit") int limit,HttpServletRequest request){
	    System.out.println("backContent========================"+limit);
	    vo.setMaxResult(limit);
	    EhyManagerModel man =  (EhyManagerModel) request.getSession().getAttribute("ehymanagermodel");
	    System.out.println("page:"+vo.toString());
	    List<FoodModel>contentList=foodservice.selectfoodAll(man.getMealId(),foodName,vo);
	    int totals=foodservice.selectcount(vo);
	    vo.setTotalRows(totals);
	    return new ResultMap<List<FoodModel>>("",contentList,0,totals);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(FoodModel food,String foodId){
		food.setIsva(1);
		food.setFoodId(foodId);
		foodservice.deleteFood(food);
		return "food2" ;
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public FoodModel findById(String foodId){
		FoodModel vo = foodservice.selectByPrimaryKey(foodId);
		return vo;
	}
	
	@RequestMapping("/add")
	public String add(
	@RequestParam(value = "pic", required = false)
	MultipartFile file,
	FoodVo vo,
	HttpServletRequest request) throws IllegalStateException, IOException{
		//使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		//获取文件的扩展名
		String ext = FilenameUtils.getExtension(file.getOriginalFilename());
		//设置图片上传路径
		String url = request.getSession().getServletContext().getRealPath("/upload");
		System.out.println(url);
		//以绝对路径保存重名命后的图片
		file.transferTo(new File(url+"/"+name + "." + ext));
		//把图片存储路径保存到数据库
		vo.setFoodImg("upload/"+name + "." + ext);
		System.out.println("增加进来");
		vo.setFoodId(Tools.getRandomString());
		EhyManagerModel man =  (EhyManagerModel) request.getSession().getAttribute("ehymanagermodel");
		vo.setMealId(man.getMealId());
		foodservice.insert(vo);
		/*mv.setViewName("redirect:/pages/food/food2.html");*/
		return "redirect:/pages/food/food2.html";
	}
	@RequestMapping("/update")
	public String update (@RequestParam(value = "pic", required = false)
	MultipartFile file,FoodModel food,HttpServletRequest request) throws IllegalStateException, IOException{
	//使用UUID给图片重命名，并去掉四个“-”
	String name = UUID.randomUUID().toString().replaceAll("-", "");
	//获取文件的扩展名
	String ext = FilenameUtils.getExtension(file.getOriginalFilename());
	//设置图片上传路径
	String url = request.getSession().getServletContext().getRealPath("/upload");
	System.out.println(url);
	//以绝对路径保存重名命后的图片
	file.transferTo(new File(url+"/"+name + "." + ext));
	//把图片存储路径保存到数据库
	food.setFoodImg("upload/"+name + "." + ext);
	foodservice.updateByPrimaryKey(food);
	return "redirect:/pages/orderbackground/food2.html";
	}
	
}
